home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 033a / fatcat20.zip / FATCAT.C < prev    next >
Text File  |  1992-01-12  |  10KB  |  381 lines

  1. /* FATCAT(C) by Bill Buckels and George Boudreau
  2.              Version 2.0 final beta
  3.              January 10,1992
  4.  
  5.    F ile
  6.    a
  7.    t
  8.    C ounter
  9.    a
  10.    t
  11.  
  12.  
  13.    This Utility updates the File Counter in The Various Files.BBS
  14.    From Download or FREQ entries in FD.LOG or RA.LOG.
  15.  
  16.    If the Download/FREQ log entry has been updated by FATCAT
  17.    a new log entry has been inserted before the original RA or FD
  18.    entry so FATCAT knows that it has already Updated the FILES.BBS
  19.    stats for the Download or the FREQ.
  20.  
  21.    FATCAT simply skips the entry if word "FATCAT(C)" is not found on
  22.    the line preceding the Download or the FREQ.
  23.  
  24.    If the FREQ has not yet been DOCUMENTED by FATCAT the appropriate
  25.    FILES.BBS stats are updated and then FATCAT Inserts a Marker
  26.    Into The FD.LOG to remind itself that this has been done.
  27.  
  28.    No Callers log is created or updated.
  29.    The RA.LOG or FD.LOG is not removed and is simply Updated.
  30.  
  31.    BTW: No warranty or liability is assumed by me.
  32.         If you don't like FATCAT then don't use it.
  33.  
  34. */
  35.  
  36.  
  37. #include <string.h>
  38. #include <stdio.h>
  39.  
  40. /* While reading the log if a "Download" is found or if a "FREQ" */
  41. /* is found to be "Sent"... */
  42. /* the following function updates the stats for "filename" to    */
  43. /* "listname" while reading "tempname" ("tempname" is the old    */
  44. /* "FILES.BBS"). After updating the stats for "filename"         */
  45. /* "tempname" is removed and we return to continue reading the   */
  46. /*  RA or FD log */
  47.  
  48. int updatefiles(char *filename, char *listname, char *tempname)
  49. {
  50.  
  51.     FILE *fp,*fp2;
  52.     char *ptr;
  53.     char temp[256];
  54.     char inbuffer[256];
  55.     char outbuffer[256];
  56.  
  57.     char *temptr;
  58.     int i;
  59.  
  60.    /* if we have a problem opening the FILES.BBS for writing */
  61.    /* we get outa here real fast... */
  62.  
  63.    if((fp=fopen(listname,"w"))==NULL)
  64.    {
  65.       fcloseall();
  66.       perror(listname);
  67.       rename(tempname,listname);
  68.       exit(0);
  69.     }
  70.  
  71.    /* if we can't read the original we do a quick clean-up */
  72.    /* and we're outa here... */
  73.  
  74.    if((fp2=fopen(tempname,"r"))==NULL)
  75.    {
  76.      fcloseall();
  77.      perror(tempname);
  78.      remove(listname);
  79.      rename(tempname,listname);
  80.      exit(0);
  81.     }
  82.  
  83.    /* we read every file in the list and look for our match */
  84.  
  85.    while(fgets(inbuffer,256,fp2)!=NULL)
  86.    {
  87.  
  88.        strcpy(outbuffer,inbuffer);
  89.        temptr=strtok(inbuffer," [");
  90.        sprintf(temp,"%s",inbuffer);
  91.  
  92.        /* string compare ignore case */
  93.  
  94.          if(strcmpi(filename,temp)==0)
  95.          {
  96.             ptr=(char *)&outbuffer[14];
  97.             i=atoi(ptr);
  98.             i++;
  99.             if(i>99)
  100.             {
  101.               if(i>999)strcpy(temp,"[999]");   /* we stop at 999 */
  102.               else sprintf(temp,"[%003d]",i);
  103.               }
  104.             if(i<100)sprintf(temp,"[%002d] ",i);
  105.  
  106.             outbuffer[13]=temp[0];
  107.             outbuffer[14]=temp[1];
  108.             outbuffer[15]=temp[2];
  109.             outbuffer[16]=temp[3];
  110.             outbuffer[17]=temp[4];
  111.  
  112.             printf("%s",outbuffer);
  113.             }
  114.  
  115.        fputs(outbuffer,fp);
  116.     }
  117.  
  118.     fclose(fp2);
  119.     fclose(fp);
  120.     remove(tempname);
  121.     return 0;
  122.  
  123. }
  124.  
  125.  
  126. /* the following function processes the RA.LOG or FD.LOG looking      */
  127. /* for Downloads or FREQs that have been "Sent" but not DOCUMENTED.   */
  128.  
  129. /* If so then "updatefiles" is called to update the FILES.BBS */
  130. /* counter in the appropriate directory and a FATCAT marker   */
  131. /* is inserted ahead of the "Download" or FREQ & "Sent" log entries. */
  132.  
  133.  
  134. int downloaded(char *original_log, char *templog)
  135. {
  136.  
  137.     FILE *fp,*fp2;
  138.  
  139.     char *ptr,*wordptr;
  140.     char stringbuf[256];
  141.     char backbuf[256],backbuf2[256];
  142.     char stringptr[256];
  143.     char statsptr[256];
  144.     char temptr[256];
  145.  
  146.     unsigned temp,checkit;
  147.  
  148.     /* rename and open the RA.LOG or FD.LOG for reading */
  149.     rename(original_log,templog);
  150.     if((fp=fopen(templog,"r"))==NULL)
  151.     {
  152.         /* nothing to do */
  153.         perror(templog);
  154.         exit(0);
  155.     }
  156.  
  157.     /* open the TEMPORARY.LOG for writing as RA.LOG or FD.LOG */
  158.     if((fp2=fopen(original_log,"w"))==NULL)
  159.     {
  160.         perror(original_log);
  161.         fclose(fp);
  162.         rename(templog,original_log);
  163.         exit(0);
  164.     }
  165.  
  166.  
  167. #undef  OFF
  168. #define OFF   0
  169. #undef  FDLOG
  170. #define FDLOG 1
  171. #undef  RALOG
  172. #define RALOG 2
  173.  
  174.   /* read until we can't read any more */
  175.   while(fgets(stringbuf,256,fp)!=NULL)
  176.   {
  177.  
  178.     /* set the download-freq flag to "OFF" */
  179.  
  180.     checkit=OFF;
  181.  
  182.     /* if we find a FATCAT Log Marker We Read and Write the Next Entry */
  183.     /* and skip the parts that Check Further since the entry counter   */
  184.     /* has been updated already in the appropriate FILES.BBS           */
  185.  
  186.     if((ptr = strstr(stringbuf,"FATCAT(C)")) != NULL)
  187.     {
  188.      fputs(stringbuf,fp2);
  189.      if(fgets(stringbuf,256,fp)==NULL)goto JUMPLABEL;
  190.      fputs(stringbuf,fp2);
  191.      goto JUMPLABEL;
  192.      }
  193.  
  194.     /* make a copy of the entry... we may need this */
  195.     strcpy(backbuf,stringbuf);
  196.  
  197.     /* if we find a FREQ set the freq flag */
  198.     if((ptr = strstr(stringbuf,"FREQ")) != NULL)checkit=FDLOG;
  199.     /* if we find a Download set the download flag */
  200.     if((ptr = strstr(stringbuf,"Download")) != NULL)checkit=RALOG;
  201.  
  202.     /* if the log entry Does Not Match The Search Criteria  */
  203.     /* it is written out again and the next part is skipped */
  204.     if(checkit==OFF)
  205.     {
  206.      fputs(stringbuf,fp2);
  207.      goto JUMPLABEL;
  208.      }
  209.  
  210.     /* If it is an RA.LOG entry... */
  211.     if(checkit==RALOG)
  212.     {
  213.         strcpy(stringptr,"");
  214.         while(*ptr++!=NULL)
  215.         {
  216.           if(ptr[0]==':')
  217.           {
  218.             *ptr--;
  219.             strcpy(stringptr,ptr);
  220.             /* get the colon out of the path */
  221.             *ptr++;
  222.           }
  223.           }
  224.           /* display filename */
  225.           wordptr=strtok(stringptr," \n");
  226.           printf("Adjusting Download Stats for %s.\n",stringptr);
  227.  
  228.           /* display file list */
  229.           strcpy(statsptr,stringptr);
  230.  
  231.           temp=strlen(statsptr);
  232.           wordptr=(char *)&statsptr[temp];
  233.  
  234.           while(*wordptr != '\\')*wordptr--;
  235.           *wordptr='\x00';
  236.           *wordptr++;
  237.           strcpy(stringptr,wordptr);
  238.  
  239.           strcpy(temptr,statsptr);
  240.           strcat(temptr,"\\TEMP.BBS");
  241.           strcat(statsptr,"\\FILES.BBS");
  242.  
  243.          if(rename(statsptr,temptr))
  244.             {
  245.              fprintf(fp2,
  246.              "----------  FATCAT(C) Could Not Update Stats %s\n",
  247.                      statsptr);
  248.              fputs(backbuf,fp2);
  249.              }
  250.           else
  251.           {
  252.             updatefiles(stringptr,statsptr,temptr);
  253.             fprintf(fp2,
  254.             "----------  FATCAT(C) Updated Stats Re: %s\n",
  255.                          statsptr);
  256.             fputs(backbuf,fp2);
  257.             }
  258.  
  259.         }
  260.  
  261.  
  262.     /* Start update FD.LOG.... */
  263.  
  264.     if(checkit==FDLOG)
  265.     {
  266.         if(fgets(stringbuf,256,fp)==NULL)goto JUMPLABEL;
  267.         /* make a copy of the entry... we may need this */
  268.         strcpy(backbuf2,stringbuf);
  269.  
  270.  
  271.          /* if the next line says "Sent" it is a valid entry        */
  272.          /* otherwise we jump past the rest of this...              */
  273.         if((ptr = strstr(stringbuf,"Sent"))== NULL)
  274.         {
  275.             fputs(backbuf,fp2);
  276.             fputs(stringbuf,fp2);
  277.             goto JUMPLABEL;
  278.             }
  279.  
  280.         strcpy(stringptr,"");
  281.         while(*ptr++!=NULL)
  282.         {
  283.           if(ptr[0]==':')
  284.           {
  285.             *ptr--;
  286.             strcpy(stringptr,ptr);
  287.             /* get the colon out of the path */
  288.             *ptr++;
  289.           }
  290.           }
  291.           /* display filename */
  292.           wordptr=strtok(stringptr,"; \n");
  293.           printf("Adjusting Download Stats for %s.\n",stringptr);
  294.  
  295.           /* display file list */
  296.           strcpy(statsptr,stringptr);
  297.  
  298.           temp=strlen(statsptr);
  299.           wordptr=(char *)&statsptr[temp];
  300.  
  301.           while(*wordptr != '\\')*wordptr--;
  302.           *wordptr='\x00';
  303.           *wordptr++;
  304.           strcpy(stringptr,wordptr);
  305.  
  306.           strcpy(temptr,statsptr);
  307.           strcat(temptr,"\\TEMP.BBS");
  308.           strcat(statsptr,"\\FILES.BBS");
  309.  
  310.           if(rename(statsptr,temptr))
  311.             {
  312.              fprintf(fp2,
  313.              "----------  FATCAT(C) Could Not Update Stats %s\n",
  314.                      statsptr);
  315.              fputs(backbuf,fp2);
  316.              fputs(backbuf2,fp2);
  317.              }
  318.           else
  319.           {
  320.             updatefiles(stringptr,statsptr,temptr);
  321.             fprintf(fp2,
  322.             "----------  FATCAT(C) Updated Stats Re: %s\n",
  323.                          statsptr);
  324.             fputs(backbuf,fp2);
  325.             fputs(backbuf2,fp2);
  326.             }
  327.  
  328.         }
  329.  
  330.  /* LOOK OUT BELOW!!!
  331.    |\/\/\/|
  332.    |      |
  333.    |      |
  334.    | (o)(o)
  335.   C      _)
  336.    | ,___|  Labels Should Be Clearly Marked If A Goto is Used!
  337.    |   /
  338.   /____\
  339.  / BART \
  340.  */
  341.  JUMPLABEL:;  /* jumps to here if we've already updated stats  */
  342.  
  343.     }
  344.  
  345.     /* close all files and remove the old RA.LOG or FD.LOG */
  346.     fcloseall();
  347.     remove(templog);
  348.  
  349.     return 0;
  350.  
  351. }
  352.  
  353.  
  354.  
  355.  
  356. main(int argc, char **argv)
  357. {
  358.    puts(
  359.    "FATCAT(C) Version 2.0ß (Final) Copyright 1992 Headboard International");
  360.    puts(
  361.    "Written by Bill Buckels and George Boudreau");
  362.  
  363.    if(argc!=2)
  364.    {
  365.      puts(
  366.      "Download Stat Update Utility for RA.LOG or FD.LOG as used with RA");
  367.      puts(
  368.      "\nUsage is \"FATCAT [ (DRIVE:\\PATHNAME\\) FD.LOG or RA.LOG]\"");
  369.      puts(
  370.      "Run from the command line or a batch file.");
  371.      puts(
  372.      "\n Use and Copy Freely!");
  373.     }
  374.    else
  375.       downloaded(argv[1],"FATCAT.$$$");
  376.  
  377.    puts(" Have a Nice Dos!");
  378.    exit(0);
  379.  
  380. }
  381.